﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2010, hrondra
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fluid.Controls;
using Close;
using Close.Controls;
using Close.Classes;

namespace Close
{
    public partial class MainForm : Form
    {
        private int _currentGameLevel;

        public MainForm()
        {
            InitializeComponent();
            if (ThemeClass.Settings.AutoSizeHeight || ThemeClass.Settings.AutoSizeWidth)
            {

                try
                {

                    if (ThemeClass.Settings.AutoSizeHeight && Height > 50)
                    {
                        if (Height > Width)
                            ThemeClass.Settings.DisplayHeight = Height;
                        else
                            ThemeClass.Settings.DisplayHeight = Width;

                    }
                    if (ThemeClass.Settings.AutoSizeWidth && Width > 50)
                    {
                        if (Height > Width)
                            ThemeClass.Settings.DisplayWidth = Width;
                        else
                            ThemeClass.Settings.DisplayWidth = Height;
                    }
                }
                catch (Exception)
                {
                    //only a feature
                }
            }


            Host.Bounds = Host.ClientBounds;
#if DEBUG
            StartGame(3);
            return;
#endif

            OpenLevelPanel();

        }

        /// <summary>
        /// User selected difficulty - show games.
        /// </summary>
        private void levelMenu_ItemClick(object sender, ListBoxItemEventArgs e)
        {
            int level = ((LevelsPanel)sender).SelectedIndex + 1;
            ((LevelsPanel)sender).Close();
            ((LevelsPanel)sender).Dispose();
            OpenGamePanel(level);
        }


        /// <summary>
        /// Open menu to select difficulty level.
        /// </summary>
        private void OpenLevelPanel() 
        {
            LevelsPanel levelMenu = new LevelsPanel();
            levelMenu.ShowMaximized();
            levelMenu.ItemClick += new EventHandler<ListBoxItemEventArgs>(levelMenu_ItemClick);            
        }

        /// <summary>
        /// Show list of games.
        /// </summary>
        /// <param name="level">Selected difficulty</param>
        private void OpenGamePanel(int level)
        {
            _currentGameLevel = level;
            GamesPanel gameMenu = new GamesPanel(level);
            gameMenu.BindData(); //HACK: init is called before constructor - do it better
            gameMenu.ShowMaximized();
            gameMenu.ButtonBackClicked += new EventHandler<EventArgs>(levelMenu_ButtonBackClicked);
            gameMenu.ItemClick += new EventHandler<ListBoxItemEventArgs>(gameMenu_ItemClick);
        }

        /// <summary>
        /// Clicked in menu - start the selected game.
        /// </summary>
        void gameMenu_ItemClick(object sender, ListBoxItemEventArgs e)
        {
            //TODO: own object
            String selectedCaption = ((GamesPanel)sender).SelectedText;
            ushort gid = ushort.Parse(selectedCaption.Split(' ')[0]);

            ((GamesPanel)sender).Close();
            ((GamesPanel)sender).Dispose();
            StartGame(gid);
        }

        /// <summary>
        /// Back from games menu to difficulty.
        /// </summary>
        void levelMenu_ButtonBackClicked(object sender, EventArgs e)
        {
            ((GamesPanel)sender).Close();
            ((GamesPanel)sender).Dispose();
            OpenLevelPanel();
        }
        

        /// <summary>
        /// Start/continue in selected game.
        /// </summary>
        /// <param name="gid">Game id.</param>
        void StartGame(ushort gid)
        {
            MainGui mg = new MainGui(gid);
            mg.BackColor = Color.Black;
            mg.ExitClick += new EventHandler(mg_ExitClick);
            mg.ShowMaximized(ShowTransition.None);
        }

        /// <summary>
        /// Exit from the played game.
        /// </summary>
        /// <param name="sender">main game panel</param>
        /// <param name="e"></param>
        void mg_ExitClick(object sender, EventArgs e)
        {
            ((MainGui)sender).Close();
            ((MainGui)sender).Dispose();
            OpenGamePanel(_currentGameLevel);
        }
    }
}